package com.hilaver.dzmis.service.impl;

import java.util.Date;
import java.util.List;

import com.hilaver.dzmis.Constants;
import com.hilaver.dzmis.basicinfo.BiMeasureSpecificationItem;
import com.hilaver.dzmis.codetable.CtCostumeCategory;
import com.hilaver.dzmis.product.ProductIdentification;
import com.hilaver.dzmis.product.ProductTimeLine;
import com.hilaver.dzmis.service.AbstractBaseService;
import com.hilaver.dzmis.util.SimpleObj2XML;
import com.hilaver.dzmis.util.StringUtils;

public class ProductTimeLineServiceImpl extends AbstractBaseService {
	
	public final static String COLUMN_PI_ID = "piId";
	
	public final static String COLUMN_STATE = "state";
	
	public final static String COLUMN_PI_ID_FULL = "productIdentification.id";
	
	public final static String COLUMN_IS_EFFECTIVE = "isEffective";
	
	public final static String COLUMN_IS_FINISHED = "isFinished";
	
	public final static String COLUMN_ALARM_TIME = "alarmTime";
	
	public final static String STATE_INEFFECTIVE = "1";
	
	public final static String STATE_FINISHED = "2";
	
	public final static String STATE_ONGOING = "3";
	
	public final static String STATE_ABNORMAL = "4";
	
	public final static String STATE_ALARM = "5";
	
	
	
	private ProductIdentificationServiceImpl piServiceImpt;
	
	public ProductTimeLineServiceImpl(){
		super();
		this.piServiceImpt = new ProductIdentificationServiceImpl();
	}
	@Override
	public String delete(int id) throws Exception {
		return super.delete(ProductTimeLine.class.getName(), id);
	}

	@Override
	public String get(int id) throws Exception {
		ProductTimeLine obj = (ProductTimeLine)this.dao.get(ProductTimeLine.class.getName(), id);
		StringBuffer xmlSB = new StringBuffer();
		xmlSB.append(Constants.XML_DOC_DEFINE);
		xmlSB.append(Constants.XML_ROOT_TAG[0]);
		xmlSB.append(Constants.XML_ITEM_TAG[0]);
		xmlSB.append(SimpleObj2XML.toXMLFragment(obj));
		xmlSB.append(Constants.XML_ITEM_TAG[1]);
		xmlSB.append(Constants.XML_ROOT_TAG[1]);
		return xmlSB.toString();
	}

	public String getAllPagination(int page, String sort, String order,
			String[] filter) throws Exception {
		this.piServiceImpt.setLocale(this.locale);
		int offset = Constants.PAGE_SIZE * (page - 1);
		
		if("id".equals(sort)){
			sort = COLUMN_STATE;
			order = Constants.ORDER_DESC;
		}
		List<ProductTimeLine> objList = this.dao.getAllPagination(ProductTimeLine.class
				.getName(), offset, Constants.PAGE_SIZE, sort, order, filter);

		StringBuffer xmlSB = new StringBuffer();
		xmlSB.append(Constants.XML_DOC_DEFINE);
		xmlSB.append(Constants.XML_ROOT_TAG[0]);
		for (ProductTimeLine obj : objList) {
			obj.setState(this.getState(obj));
			this.edit(obj);
			
			xmlSB.append(Constants.XML_ITEM_TAG[0]);
			xmlSB.append(Constants.XML_INDEX_TAG[0]);
			xmlSB.append(++offset);
			xmlSB.append(Constants.XML_INDEX_TAG[1]);

			xmlSB.append(SimpleObj2XML.toXMLFragment(obj));

			xmlSB.append("<" + COLUMN_PI_ID + ">");
			xmlSB.append(obj.getProductIdentification().getId());
			xmlSB.append("</" + COLUMN_PI_ID + ">");
			
			xmlSB.append(this.piServiceImpt.toXMLFragment(obj.getProductIdentification()));
			xmlSB.append(Constants.XML_ITEM_TAG[1]);
		}

		xmlSB.append(Constants.XML_CURRENT_PAGE_TAG[0]);
		xmlSB.append(page);
		xmlSB.append(Constants.XML_CURRENT_PAGE_TAG[1]);
		xmlSB.append(Constants.XML_ITEM_TOTAL_TAG[0]);
		xmlSB.append(this.dao.getCount(ProductTimeLine.class.getName(),
				filter));
		xmlSB.append(Constants.XML_ITEM_TOTAL_TAG[1]);
		xmlSB.append(Constants.XML_ROOT_TAG[1]);
		return xmlSB.toString();
	}
	@Override
	public String getAllPagination(int page, String sort, String order)
			throws Exception {
		// TODO Auto-generated method stub
		return null;
	}
	
	public String getAll(Integer piId) throws Exception{
		String [] filters = new String[1];
		filters[0] = COLUMN_PI_ID_FULL + "=" + piId;
		List<ProductTimeLine> objList = this.dao.getAll(ProductTimeLine.class.getName(), COLUMN_ID, "asc", filters);
		StringBuffer xmlSB = new StringBuffer();
		xmlSB.append(Constants.XML_DOC_DEFINE);
		xmlSB.append(Constants.XML_ROOT_TAG[0]);
		int offset = 0;
		for (ProductTimeLine obj : objList) {
			
			obj.setState(this.getState(obj));
			this.edit(obj);
			
			xmlSB.append(Constants.XML_ITEM_TAG[0]);
			xmlSB.append(Constants.XML_INDEX_TAG[0]);
			xmlSB.append(++offset);
			xmlSB.append(Constants.XML_INDEX_TAG[1]);
			xmlSB.append(SimpleObj2XML.toXMLFragment(obj));
			xmlSB.append(Constants.XML_ITEM_TAG[1]);
		}
		xmlSB.append(Constants.XML_CURRENT_PAGE_TAG[0]);
		xmlSB.append("1");
		xmlSB.append(Constants.XML_CURRENT_PAGE_TAG[1]);

		xmlSB.append(Constants.XML_ITEM_TOTAL_TAG[0]);
		xmlSB.append(this.dao.getCount(ProductTimeLine.class.getName(),filters));
		xmlSB.append(Constants.XML_ITEM_TOTAL_TAG[1]);
		xmlSB.append(Constants.XML_ROOT_TAG[1]);
		return xmlSB.toString();
	}	
	
	private String getState(ProductTimeLine ptl){
		if(("02").equals(ptl.getIsEffective())){
			return STATE_INEFFECTIVE;
		}
		if("01".equals(ptl.getIsFinished())){
			return STATE_FINISHED;
		}
		Date alarmDate = ptl.getAlarmTime();
		Date finishedDate = ptl.getFinishedTime();
		Date now = new Date();
		if(now.getTime() >= finishedDate.getTime() || finishedDate.getTime() <= alarmDate.getTime()){
			return STATE_ABNORMAL;
		}
		if(now.getTime() >= alarmDate.getTime()){
			return STATE_ALARM;
		}else{
			return STATE_ONGOING;
		}
		
	}
	
	public static void main(String[] args) throws Exception{
		ProductTimeLineServiceImpl os = new ProductTimeLineServiceImpl();
		System.out.println(os.getAll(20));
		//char index = 'C';
		//System.out.println(++index);
	}
}



